package cn.agiledata.bank.common.tag;

import javax.servlet.jsp.JspException;

import org.apache.struts.taglib.TagUtils;
import org.apache.struts.taglib.html.TextTag;

public class CalendarTag extends TextTag  {
	private static final long serialVersionUID = -6386007650554756745L;
	
	private String format;   // 日期格式
	private String align;    // 位置 
	private String path;     //path地址
	private String calendarType;  //日期类型：'beforeToday':只允许输入小于今天的日期
	


	/**
     * Construct a new instance of this tag.
     */
    public CalendarTag() {
    	 super();
    	 this.format = "yyyy-mm-dd";
    	 this.align = "left";
         this.maxlength = "10";
         this.cols = "10";
    }
	
    /**
     * Generate the required input tag.
     * <p>
     * Support for indexed property since Struts 1.1
     *
     * @exception JspException if a JSP exception has occurred
     */
    public int doStartTag() throws JspException {
        //输入框
        TagUtils.getInstance().write(this.pageContext, this.renderInputElement());
        //图片
        TagUtils.getInstance().write(this.pageContext, this.renderImage());
        return (EVAL_BODY_TAG);

    }


	/**
     * 'Hook' to enable tags to be extended and 
     *  additional attributes added.
     * @param handlers The StringBuffer that output will be appended to.
     */
    protected void prepareOtherAttributes(StringBuffer handlers) {
    	 if(calendarType == null) {
    		 prepareAttribute(handlers, "onblur", "checkDateInput(this)");
    	 } else if(calendarType.equals("beforeToday")) { //小于今天
    		 prepareAttribute(handlers, "onblur", "checkDateInput(this);calendarCompare(this,'"+calendarType+"')");
    	 }
    }
	
	/**
	 * 读取日历图片
	 * @param sb
	 * @return
	 */
	public String renderImage() {
		StringBuffer sb = new StringBuffer();
		sb.append("&nbsp;<img src='")
		  .append(getPath())
		  .append("/images/date.gif' style='cursor:hand' onclick='popUpCalendar(this, ")
		  .append(getProperty())
		  .append(", ")
		  .append(" \"")
		  .append(getFormat())
		  .append("\" ")
		  .append(",")
		  .append("left".equals(getAlign()) ? false : true)
		  .append(")'/>");
		return sb.toString();
	}
	
    
    public String getAlign() {
		return align;
	}

	public void setAlign(String align) {
		this.align = align;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getCalendarType() {
		return calendarType;
	}

	public void setCalendarType(String calendarType) {
		this.calendarType = calendarType;
	}




}
